package com.example.app15_handler.activity;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import androidx.activity.EdgeToEdge;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

import com.example.app15_handler.R;

public class HandlerDemoActivity extends AppCompatActivity implements View.OnClickListener {

    private static final String TAG = "HandlerDemoActivity";
    private static final int WHAT_INCREMENT = 1;
    private static final int WHAT_DECREMENT = 2;

    private TextView tvHandlerDemoNumber;
    private Button btnHandlerDemoIncrement;
    private Button btnHandlerDemoDecrement;
    private Button btnHandlerDemoPause;

    private Handler handler = new Handler() {
        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
            Log.i(TAG, "handleMessage: " + msg.what);
            // 获取当前显示的数值
            int number = Integer.parseInt(tvHandlerDemoNumber.getText().toString());
            switch (msg.what) {
                case WHAT_INCREMENT:
                    if (number == 20) {
                        // 限制按钮的可操作性
                        btnHandlerDemoPause.setEnabled(false);
                        Toast.makeText(HandlerDemoActivity.this, "已经到达最大值", Toast.LENGTH_SHORT).show();
                        return;
                    }
                    number++;
                    tvHandlerDemoNumber.setText(number + "");
                    // 发送增加的延迟消息
                    handler.sendEmptyMessageDelayed(WHAT_INCREMENT, 1000);
                    break;
                case WHAT_DECREMENT:
                    if (number == 1) {
                        // 限制按钮的可操作性
                        btnHandlerDemoPause.setEnabled(false);
                        Toast.makeText(HandlerDemoActivity.this, "已经到达最大值", Toast.LENGTH_SHORT).show();
                        return;
                    }
                    number--;
                    tvHandlerDemoNumber.setText(number + "");
                    // 发送减少的延迟消息
                    handler.sendEmptyMessageDelayed(WHAT_DECREMENT, 1000);
                    break;
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_handler_demo);
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
            Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
            return insets;
        });

        Log.i(TAG, "onCreate()");
        // 初始化视图
        initView();

        btnHandlerDemoIncrement.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

            }
        });
    }

    /**
     * 初始化视图对象
     */
    private void initView() {
        tvHandlerDemoNumber = findViewById(R.id.tv_handler_demo_number);
        btnHandlerDemoIncrement = findViewById(R.id.btn_handler_demo_increment);
        btnHandlerDemoDecrement = findViewById(R.id.btn_handler_demo_decrement);
        btnHandlerDemoPause = findViewById(R.id.btn_handler_demo_pause);
    }

    /**
     * 处理按钮点击事件
     * @param v The view that was clicked.
     */
    @Override
    public void onClick(View v) {
        Log.i(TAG, "onClick: " + v.getId());
        if (v.getId() == R.id.btn_handler_demo_increment) { // 自动增加
            Log.i(TAG, "onClick: 自动增加");
            // 限制按钮的可操作性
            btnHandlerDemoPause.setEnabled(true);
            btnHandlerDemoIncrement.setEnabled(false);
            btnHandlerDemoDecrement.setEnabled(true);
            // 停止减少（未处理的减少消息）
            handler.removeMessages(WHAT_DECREMENT);
            // 发送消息
            handler.sendEmptyMessage(WHAT_INCREMENT);
        } else if(v.getId() == R.id.btn_handler_demo_decrement) { // 自动减少
            Log.i(TAG, "onClick: 自动减少");
            // 限制按钮的可操作性
            btnHandlerDemoPause.setEnabled(true);
            btnHandlerDemoIncrement.setEnabled(true);
            btnHandlerDemoDecrement.setEnabled(false);

            // 停止增加（未处理的增加消息）
            handler.removeMessages(WHAT_INCREMENT);
            // 发送消息
            handler.sendEmptyMessage(WHAT_DECREMENT);
        } else if(v.getId() == R.id.btn_handler_demo_pause) { // 暂停
            Log.i(TAG, "onClick: 暂停");
            // 限制按钮的可操作性
            btnHandlerDemoPause.setEnabled(false);
            btnHandlerDemoIncrement.setEnabled(true);
            btnHandlerDemoDecrement.setEnabled(true);
            // 停止减少（未处理的减少消息）
            handler.removeMessages(WHAT_DECREMENT);
            // 停止增加（未处理的增加消息）
            handler.removeMessages(WHAT_INCREMENT);
        }
    }
}